﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FlexiCommerce.Web.Templates.Web.UI;
using FlexiCommerce.Web.Addons.FC.Items;
using System.Text.RegularExpressions;
using System.Text;
using FlexiCommerce;
namespace FlexiCommerce.Web.Addons.FC.UI.Parts
{
    public partial class CategoryData : TemplateUserControl<CategoryPage, CategoryDataItem>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StringBuilder content = new StringBuilder(CurrentItem.Content);
                if (FlexiCommerce.Context.CurrentCategory != null)
                {
                    Regex reg = new Regex("{{.*}}", RegexOptions.RightToLeft);
                    MatchCollection matches = reg.Matches(content.ToString());
                    foreach (Match match in matches)
                    {
                        string newValue = GetValue(content.ToString().Substring(match.Index, match.Length).Replace("{", "").Replace("}", ""));
                        content.Remove(match.Index, match.Length);
                        content.Insert(match.Index, newValue);
                    }
                }
                LiteralContent.Text = content.ToString();
            }
        }
        private string GetValue(string property)
        {
            //TODO: Add support for more data fields for category data item
            if (property.Equals("name", StringComparison.InvariantCultureIgnoreCase))
            {
                return FlexiCommerce.Context.CurrentCategory.Name;
            }
            throw new FlexiException("{0} does not have a property {1}",
                FlexiCommerce.Context.CurrentCategory.Name,
                property);
        }
    }
}